package com.lynu.service.impl;
import com.alibaba.druid.util.StringUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lynu.pojo.User;
import com.lynu.service.UserService;
import com.lynu.mapper.UserMapper;
import com.lynu.utils.JwtHelper;
import com.lynu.utils.MD5Util;
import com.lynu.utils.Result;
import com.lynu.utils.ResultCodeEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
* @author 31536
* @description 针对表【news_user】的数据库操作Service实现
* @createDate 2024-04-15 21:33:13
*/
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User>
    implements UserService{
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private JwtHelper jwtHelper;
    @Override
    public Result login(User user){
        String userMd5= MD5Util.encrypt(user.getUserPwd());
        User logUser=userMapper.selectByUsername(user.getUsername());
        System.out.println(logUser);
        //如果根据账号名字查不到账号，就证明账号错了
        if(logUser==null){
            return Result.build(null,ResultCodeEnum.USERNAME_ERROR);
        }

        //如果使用==判断两个字符串是否相同实际上是判断是否是同一个引用
        //使用.equal判断内容
        //查到了账号，但加密后的密码和数据库中的不一样，则证明密码错了
        if(!StringUtils.isEmpty(logUser.getUserPwd())&&logUser.getUserPwd().equals(userMd5)){
            String token=jwtHelper.createToken(Long.valueOf(logUser.getUid()));
            Map map=new HashMap();
            map.put("token",token);
            return Result.ok(map);
        }
        return Result.build(null,ResultCodeEnum.PASSWORD_ERROR);

    }
    @Override
    public Result getUserInfo(String token) {
        //通过token判断登录是否过期
        if(jwtHelper.isExpiration(token)){
            return Result.build(null,ResultCodeEnum.NOTLOGIN);
        }
//        Long userId=jwtHelper.getUserId(token);
//        User user=userMapper.selectById(userId);
        int userId=jwtHelper.getUserId(token).intValue();
        User user=userMapper.selectById(userId);
        if(user==null){
            return Result.build(null,ResultCodeEnum.NOTLOGIN);
        }
        user.setUserPwd(null);
        Map map=new HashMap();
        map.put("loginUser",user);
        return Result.ok(map);
    }

    @Override
    public Result checkUserName(String username) {
        User user=userMapper.selectByUsername(username);
        System.out.println(user);
        if(user!=null){
            return Result.build(null,ResultCodeEnum.USERNAME_USED);
        }else{
            return Result.ok(null);
        }
    }

    @Override
    public Result regist(User user) {
        String userName=user.getUsername();
        String nikeName=user.getNickName();
        String password=MD5Util.encrypt(user.getUserPwd());
        userMapper.regist(userName,password,nikeName);
        User registedUser=userMapper.selectByUsername(userName);
        if (registedUser!=null){
            return Result.ok(null);
        }
        return Result.build(null,900,"注册失败，请稍后重试");
    }
}




